package com.example.testcentral.designpattern.ObserverPattern

/**
 * File Name: WeatherData
 * Author: zhangzhenzhong
 * Created on: 2024/7/5
 * Description:被订阅者-被观察者
 */
class WeatherData : Subject {

    var observers:ArrayList<Observer> =ArrayList()
    var temperature:Float=0.0f
    var humidty:Float=0.0f

    constructor()

    fun setWeather(temperature:Float,humidty:Float){
        this.temperature=temperature
        this.humidty=humidty
        notifyObservers()
    }

    override fun registerObserver(value: Observer) {
        observers.add(value)
    }

    override fun removeObserver(value: Observer) {
        observers.remove(value)
    }

    override fun notifyObservers() {
        for (observer in observers){
            observer.update()
        }
    }

    fun  changeWeather(){
        notifyObservers()
    }


    open fun  getTemp():Float{
        return this.temperature
    }

   open fun getHumidity():Float{
        return humidty
    }

}